package model

import (
	"../encode"
	"../global"
	"github.com/go-pg/pg"
)

type AgentModel struct{}

//注册代理商资料
func (*AgentModel) RegAgent(query *Agent) (err error) {
	query.Password, err = encode.MD5ByStr(query.Password, global.Config.Section("app").Key("passwordKey").String())
	if err != nil {
		return
	}
	_, err = global.DB.Model(query).Insert()
	return
}

//获得代理商资料
func (*AgentModel) GetAgent(query *Agent, column []string, listParams *ListParams) (result []Agent, err error) {
	model := global.DB.Model(&result)
	for k := range column {
		model.Column(column[k])
	}
	if query.ID != 0 {
		model.Where("id=?", query.ID)
	}
	if query.Email != "" {
		model.Where("email=?", query.Email)
	}
	if query.Password != "" {
		query.Password, err = encode.MD5ByStr(query.Password, global.Config.Section("app").Key("passwordKey").String())
		if err != nil {
			return
		}
		model.Where("password=?", query.Password)
	}
	if query.TypeNotNull == true {
		model.Where("type=?", query.Type)
	}
	if query.TeamName != "" {
		model.Where("team_name=?", query.TeamName)
	}
	if query.WXAppID != "" {
		model.Where("wx_appid=?", query.WXAppID)
	}
	if query.WXSecret != "" {
		model.Where("wx_secret=?", query.WXSecret)
	}
	if query.FullName != "" {
		model.Where("fullname=?", query.FullName)
	}
	if query.Mobile != "" {
		model.Where("mobile=?", query.Mobile)
	}
	if query.BankNumber != "" {
		model.Where("bank_number=?", query.BankNumber)
	}
	if query.BankName != "" {
		model.Where("bank_name=?", query.BankName)
	}
	if query.LicenceNumber != "" {
		model.Where("licence_number=?", query.LicenceNumber)
	}
	if query.IDCardNumber != "" {
		model.Where("idcard_number=?", query.IDCardNumber)
	}
	model.Limit(listParams.Limit)
	if listParams.Limit != 0 && listParams.Page != 0 {
		err = model.Offset((listParams.Page - 1) * listParams.Limit).Select()
	}
	err = model.Select()

	return
}

//编辑代理商资料
func (*AgentModel) EditAgent(query *Agent) (err error) {
	model := global.DB.Model(&Agent{})
	model.Where("id=?", query.ID)

	if query.Email != "" {
		model.Set("email=?", query.Email)
	}
	if query.Password != "" {
		query.Password, err = encode.MD5ByStr(query.Password, global.Config.Section("app").Key("passwordKey").String())
		if err != nil {
			return
		}
		model.Set("password=?", query.Password)
	}
	if query.TeamName != "" {
		model.Set("team_name=?", query.TeamName)
	}
	if query.WXAppID != "" {
		model.Set("wx_appid=?", query.WXAppID)
	}
	if query.WXSecret != "" {
		model.Set("wx_secret=?", query.WXSecret)
	}
	if query.FullName != "" {
		model.Set("fullname=?", query.FullName)
	}
	if query.Mobile != "" {
		model.Set("mobile=?", query.Mobile)
	}
	if query.BankNumber != "" {
		model.Set("bank_number=?", query.BankNumber)
	}
	if query.BankName != "" {
		model.Set("bank_name=?", query.BankName)
	}
	if query.LicenceNumber != "" {
		model.Set("licence_number=?", query.LicenceNumber)
	}
	if query.IDCardNumber != "" {
		model.Set("idcard_number=?", query.IDCardNumber)
	}
	if query.LicencePhoto != "" {
		model.Set("licence_photo=?", query.LicencePhoto)
	}
	if query.IDCardFrontPhoto != "" {
		model.Set("idcard_front_photo=?", query.IDCardFrontPhoto)
	}
	if query.IDCardBackPhoto != "" {
		model.Set("idcard_back_photo=?", query.IDCardBackPhoto)
	}
	_, err = model.Update()
	return
}

//删除代理商
func (*AgentModel) DelAgent(IDs []int64) (err error) {
	if len(IDs) > 1 {
		_, err = global.DB.Model(&Agent{}).WhereIn("id in (?)", pg.In(IDs)).Delete()
	} else {
		_, err = global.DB.Model(&Agent{}).Where("id=?", IDs[0]).Delete()
	}
	return
}
